#include "DirectorLoader.h"
#include "PlayerPerformance.h"

using namespace cocos2d;
//using namespace CocosDenshion;

//===== CONSTRUCTOR =====
PlayerPerformance::PlayerPerformance(int totalGameDuration)
{
	_isDamaged = false;

	_gameDuration = _aliveDuration = 0;

	_totalGameDuration = ((float) totalGameDuration)/60.0;

	setEquationVariable();
	setGameEquationVariable();
}

//===== DESTRUCTOR =====
PlayerPerformance::~PlayerPerformance(void)
{

}

//===== PUBLIC FUNCTIONS =====

/*!
    @function updateDuration
    @param		timeDiff: float
				time difference since the last call
    @result		updates duration of counters
 */
void PlayerPerformance::update(float timeDiff)
{
	_gameDuration += timeDiff;
	_aliveDuration += timeDiff;
}

/*!
    @function resetAliveDuration
    @result		reset alive duration when character is damaged
 */
void PlayerPerformance::reduceDifficulty()
{
	_isDamaged = true;
	_aliveDuration = 0;
}

/*!
    @function getPerformanceLevel
    @result		gets the difficulty level based on character alive duration
    			returns -1 if character damaged
 */
int	PlayerPerformance::getPerformanceLevel()
{
	int performance = getAliveDurationDiff() + getGameDurationDiff();

	CCLog("Player's Performance: %i", performance);

	if( performance > 0)
	{
		return performance;
	}

	return 0;
}

/*!
    @function isDamaged
    @result		true - character is damaged, Director half difficulty
    			false - not damaged, player performing well
 */
bool PlayerPerformance::isDifficultyReduced()
{
	if( _isDamaged )
	{
		_isDamaged = false;
		return true;
	}

	return false;
}


//===== PRIVATE FUNCTIONS =====

/*!
    @function setEquationVariable
    @result		set variable K and C to be used in the equation, y = ke^x + c
 */
void PlayerPerformance::setEquationVariable()
{
	float POINT_Y1 = DirectorLoader::PLAYER_PERFORMANCE_Y1;		// y-pos in eqn
	float POINT_Y2 = DirectorLoader::PLAYER_PERFORMANCE_Y2;

	float POINT_X1 = DirectorLoader::PLAYER_PERFORMANCE_X1;		// x-pos in eqn
	float POINT_X2 = DirectorLoader::PLAYER_PERFORMANCE_X2;

	_varK = DirectorLoader::getPosLogVarK(POINT_Y1, POINT_Y2, POINT_X1, POINT_X2);
	_varC = DirectorLoader::getPosLogVarC(_varK, POINT_Y1, POINT_X1);
}

/*!
    @function setGameEquationVariable
    @result		set variable K and C to be used in the equation, y = ke^x + c
    			Adds more difficulty as game progresses
 */
void PlayerPerformance::setGameEquationVariable()
{
	float POINT_Y1 = DirectorLoader::PLAYER_PERFORMANCE_GAME_Y1;		// y-pos in eqn
	float POINT_Y2 = DirectorLoader::PLAYER_PERFORMANCE_GAME_Y2;

	float POINT_X1 = _totalGameDuration - _totalGameDuration*2.0/5.0;	// x-pos in eqn
	float POINT_X2 = _totalGameDuration - _totalGameDuration/10.0;

	_varGameK = DirectorLoader::getPosLogVarK(POINT_Y1, POINT_Y2, POINT_X1, POINT_X2);
	_varGameC = DirectorLoader::getPosLogVarC(_varGameK, POINT_Y1, POINT_X1);
}

/*!
    @function getAliveDurationDiff
    @result		get the difficulty level based on how long character is alive
    			y -> difficulty increment at each callback
    			x -> alive duration in minutes
 */
int	PlayerPerformance::getAliveDurationDiff()
{
	float result = DirectorLoader::getPosLogYValue(_varK, _varC, _aliveDuration/60.0);
	return ( result < DirectorLoader::PLAYER_PERFORMANCE_Y1 ) ? DirectorLoader::PLAYER_PERFORMANCE_Y1 : result;
}

/*!
    @function getGameDurationDiff
    @result		get the difficulty level based on game length
    			y -> difficulty increment at each callback
    			x -> duration in minutes
 */
int	PlayerPerformance::getGameDurationDiff()
{
	float result = DirectorLoader::getPosLogYValue(_varGameK, _varGameC, _gameDuration/60.0);
	return ( result < DirectorLoader::PLAYER_PERFORMANCE_GAME_Y1 ) ? 0 : result;
}
